<?php

class M_jabatan extends CI_Model{
	
	function M_jabatan(){
		parent::__construct();
	}
	
	function get_list(){
		$query = "SELECT jabatan.KODEUNIT AS KODEUNIT, unitkerja.NAMAUNIT AS NAMAUNIT, KODEJAB, NAMAJAB, HITUNGLEMBUR, KOMPENCUTI
		FROM jabatan JOIN unitkerja ON(unitkerja.KODEUNIT = jabatan.KODEUNIT)";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($kodeunit, $kodejab, $namajab, $hitunglembur, $kompencuti){
		$sql = "SELECT KODEUNIT, KODEJAB FROM jabatan WHERE KODEUNIT='".$kodeunit."' AND KODEJAB='".$kodejab."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"KODEUNIT"=>$kodeunit,
					"KODEJAB"=>$kodejab,
					"NAMAJAB"=>$namajab,
					"HITUNGLEMBUR"=>$hitunglembur,
					"KOMPENCUTI"=>$kompencuti
			);
			$this->db->insert('jabatan', $data);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 0;
			}
		}
	}
	
	function update($kodeunit_lama, $kodeunit, $kodejab_lama, $kodejab, $namajab, $hitunglembur, $kompencuti){
		$data = array(
				"KODEJAB"=>$kodejab,
				"NAMAJAB"=>$namajab,
				"HITUNGLEMBUR"=>$hitunglembur,
				"KOMPENCUTI"=>$kompencuti
		);
		
		//check kodeunit ==> jika ada dalam table.jabatan.KODEUNIT maka terjadi perubahan
		if($kodeunit != ''){
			$sql_jabatan = "SELECT KODEUNIT FROM jabatan WHERE KODEUNIT=".$kodeunit;
			$rs_jabatan = $this->db->query($sql_jabatan);
			if ($rs_jabatan->num_rows() > 0){
				$data["KODEUNIT"] = $kodeunit;
			}
		}
		
		$this->db->where('KODEUNIT', $kodeunit_lama);
		$this->db->where('KODEJAB', $kodejab_lama);
		$this->db->update('jabatan', $data);
		
		return 1;
	}
	
}

?>